package com.wipz.eoa.iphone.businessProcess;

import java.util.HashMap;
import java.util.Map;

import com.toft.core2.ToftException;

/**
 * 业务处理核心类
 * @author Administrator
 *
 */
public class BusinessCoreServices extends WorkFlowCoreService{

	
	/**
	 * 提交
	 * paramsMap:页面传来的参数
	 * paramsObj:根据具体流程需要的参数，自己通过拼装,key:value的形式
	 * businessID:业务类型ID
	 * operationFlag:调用的执行语句字符串
	 * @throws ToftException
	 */
	/*public void doSubmit(Map paramsMap,Map putMap,String businessID,String operationFlag,UserInfo userInfo,String real_agency,String PROCESSID) {
		String nextApprovaler = "";
		//更新单据相关信息
		Map params = paramsMap;
		//单据ID 用于在流程中追踪单据用
		String id = (String) params.get("ID");
		//流程所需参数(用户ID、用户编号、用户所属部门编号、用户所属单位编号)
		String userID = userInfo.getUserId();
		String userCode = userInfo.getUserRealCode();
		String deptID = userInfo.getOrgId();
		//String unitID = this.getContext().getUserInfo().getUnitId();
		String unitID = real_agency;
		//业务类型ID
		String businessid = businessID;
		if ("".equals(id)) {
			//throw ToftException.ThrowToftException(getErrorText("TOFT_11106", new Object[] { "id=" + id }));
		}
		//流程编号，如果存在则为重新提交，如果为null则为新单据
		String processinstanceID = (String)params.get("processInstanceId");
		//执行任务后的结果参数
		CompleteTaskResult completeTaskResult = null;
		Map newParams = new HashMap();
		Iterator iterator = putMap.entrySet().iterator();
		while(iterator.hasNext()){
			Entry entry = (Entry)iterator.next();
			newParams.put(entry.getKey(), entry.getValue());
		}
		//提交/二次提交
		try {
			if(processinstanceID != null && !"".equals(processinstanceID) ){//二次提交
				completeTaskResult = super.reSubmitProcess(unitID, businessid, newParams, userCode);	
			}else{//首次提交		
				completeTaskResult = super.submitProcess(unitID, businessid, newParams, userCode);	
				processinstanceID = (String)completeTaskResult.getProcessInstanceId();	 //获得工作流实例ID
				params.put("assignee", userID);
				params.put("modifydepartment", deptID);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String currentDate = sdf.format(new Date());
				params.put("createtime", currentDate);	//提交时间
				params.put("processInstanceId", processinstanceID);
				
				Map processMap = new HashMap();
				processMap.put("BILLID", params.get("BILLCODE"));
				processMap.put("PROCESSID", processinstanceID);
				processMap.put("BUSINESSTYPE", businessID);
				super.saveProcess(processMap);
			}
			
			//查询下级审批人
			nextApprovaler = (String)completeTaskResult.getNextOperators();
			
			// 更改单据状态为已提交待审批
			updateWorkflowProcessStatus("BEGIN",operationFlag,id,nextApprovaler,PROCESSID);
			
			//当存在下级处理人时,传入下级处理人信息,不存在时,单据审批结束
			if (!"".equals(processinstanceID)) {
				if(nextApprovaler != null && !"".equals(nextApprovaler)){
					this.getContext().dataResult.setMessage(nextApprovaler);
				}else{
					if(completeTaskResult.isEnded()){
						updateWorkflowProcessStatus("END",operationFlag,id,"");
						this.getContext().dataResult.setMessage("true");
					}
				}
			} else {
				this.getContext().dataResult.setMessage("false");
			}
		} catch (Exception je) {
			ApprocalAssistance.rollbackTransaction();
			je.printStackTrace();
			//throw ToftException.ThrowToftException(je.getMessage());
		}
	}*/
	
	/**
	 * 修改流程状态
	 * @param operatorFlag : 修改标识
	 * @param id : ID
	 * @param nextApprovaler ： 下级审批人
	 * @throws ToftException
	 */
	public Map updateWorkflowProcessStatus(String flag,String operationFlag,String id,String nextApprovaler,String PROCESSID) {
		Map result = new HashMap();
		int processStatus = 1;
		//流程结束
		if ("END".equals(flag)){
			processStatus = 2;
		}else if ("BACK".equals(flag)){
			processStatus = 4;
		}
		result.put("PROCESS_STATUS", processStatus);
		result.put("ID", id);
		result.put("NEXTAPPROVALER", nextApprovaler);
		super.update(operationFlag, result);
		result.put("ID", PROCESSID);
		return result;
		//this.getContext().addAttribute("result", result);
	}

}
